/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import GUI.SQLForm;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Trung
 */
public class Transporter implements Runnable{
    
    Socket socket;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    
//    List<String> lstDBNames;
//    String queryString;
//    String currentTBName;
    SQLForm queryForm;
    
    public Transporter(Socket _socket, ObjectInputStream _ois, ObjectOutputStream _oos, SQLForm _queryForm)
    {
        socket = _socket;
        ois = _ois;
        oos = _oos;
        queryForm = _queryForm;
    }
    
    public void sendAQuery(String queryString, String dbName) throws IOException
    {
        oos.writeUTF(queryString);
        oos.reset();
        
        oos.writeUTF(dbName);
        oos.reset();              
    }
    
    public DefaultTableModel receiveResult()
    {
        DefaultTableModel model = new DefaultTableModel();
        try
        {
            model = (DefaultTableModel)ois.readObject();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        catch(ClassNotFoundException cex)
        {
            System.out.print(cex.getMessage());
        }
        return model;        
    }
    
    @Override
    public void run()
    {
        try
        {
            sendAQuery(queryForm.getQueryString(), queryForm.getCurrentTBName());
            queryForm.setModel(receiveResult());
            
            queryForm.showResult();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
    
}
